package com.internetCafes.spms.web.sys.controller;

import com.internetCafes.spms.common.utils.entity.RR;
import com.internetCafes.spms.common.utils.entity.page.PageItemDTO;
import com.internetCafes.spms.common.utils.entity.page.PageItemVO;
import com.internetCafes.spms.web.sys.entity.dto.qualiconf.QualiCertDeleteDto;
import com.internetCafes.spms.web.sys.entity.dto.qualiconf.QualiConfigCertConfigDto;
import com.internetCafes.spms.web.sys.entity.dto.qualiconf.QualiConfigConfigDto;
import com.internetCafes.spms.web.sys.entity.dto.qualiconf.QualiPageDto;
import com.internetCafes.spms.web.sys.entity.vo.qualiconf.QualiConfVo;
import com.internetCafes.spms.web.sys.entity.vo.qualiconf.QualiConfigCertConfigVo;
import com.internetCafes.spms.web.sys.entity.vo.qualiconf.QualiListVo;
import com.internetCafes.spms.web.sys.entity.vo.qualiconf.QualiPageVo;
import com.internetCafes.spms.web.toolbox.entity.vo.quali.SingQualiMatchVo;
import com.internetCafes.spms.web.toolbox.service.IQualiCertCondService;
import com.internetCafes.spms.web.toolbox.service.IQualiConfService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 证书条件配置
 *
 * @author Zj 2021/6/11
 */
@Api(tags = {">>>企业资质"})
@RestController
@RequestMapping("/sys/quali")
public class QualiConfigController {

    @Autowired
    private IQualiCertCondService iQualiCertCondService;
    @Autowired
    private IQualiConfService iQualiConfService;

    @ApiOperation(value = "匹配配置的资质信息")
    @GetMapping("/matchCond/{certId}")
    public RR<List<SingQualiMatchVo>> matchCond(@PathVariable("certId") Long certId) {
        List<SingQualiMatchVo> singQualiMatchVos = iQualiConfService.matchCond(certId);
        return RR.successData(singQualiMatchVos);
    }

    /*@ApiOperation(value = "匹配所有资质信息")
    @GetMapping("/matchAll/{typeTag}")
    public RR<List<SingQualiMatchVo>> matchAll(@PathVariable("typeTag") Integer typeTag) {
        List<SingQualiMatchVo> singQualiMatchVos = iQualiConfService.batchQualiMatch(typeTag);
        return new RR().successData(singQualiMatchVos);
    }*/

    @ApiOperation(value = "匹配资质信息")
    @GetMapping("/match/{id}")
    public RR<SingQualiMatchVo> matchSingle(@PathVariable("id") Long id) {
        SingQualiMatchVo result = iQualiConfService.singQualiMatch(id);
        return RR.successData(result);
    }

    @ApiOperation(value = "资质查询列表-用户查询列表")
    @PostMapping("/show/paging")
    @RequiresPermissions("toolbox:quali:list")
    public RR<PageItemVO<QualiPageVo>> pagingForUser(@RequestBody PageItemDTO<QualiPageDto> dto) {
        dto.getConditions().setStatusFlag(1);
        Integer matchFlag = 1;
        PageItemVO<QualiPageVo> result;
        if (matchFlag.equals(dto.getConditions().getMatchFlag())) {
            result = iQualiConfService.matchAllAndPage(dto);
        } else {
            result = iQualiConfService.page(dto);
        }
        return RR.successData(result);
    }

    @ApiOperation(value = "资质查询列表-管理员配置列表")
    @PostMapping("/paging")
    @RequiresPermissions("sys:quali:list")
    public RR<PageItemVO<QualiPageVo>> paging(@RequestBody PageItemDTO<QualiPageDto> dto) {
        PageItemVO<QualiPageVo> result = iQualiConfService.page(dto);
        return RR.successData(result);
    }

    @ApiOperation(value = "资质筛选列表")
    @GetMapping("/select/list")
    @RequiresPermissions("sys:quali:list")
    public RR<List<QualiListVo>> qualiList() {
        List<QualiListVo> result = iQualiConfService.qualiList();
        return RR.successData(result);
    }

    @ApiOperation(value = "资质信息删除")
    @GetMapping("/delete/{id}")
    @RequiresPermissions("sys:quali:delete")
    public RR<Void> delete(@PathVariable("id") Long id) {
        iQualiConfService.delete(id);
        return RR.success();
    }

    @ApiOperation(value = "资质编辑保存")
    @PostMapping("/edit")
    @RequiresPermissions("sys:quali:update")
    public RR<Void> edit(@RequestBody QualiConfigConfigDto dto) {
        iQualiConfService.edit(dto);
        return RR.success();
    }

    @ApiOperation(value = "获取资质编辑信息")
    @GetMapping("/toEdit/{id}")
    @RequiresPermissions("sys:quali:update")
    public RR<QualiConfVo> toEdit(@PathVariable("id") Long id) {
        QualiConfVo qualiConfVo = iQualiConfService.toEdit(id);
        return RR.successData(qualiConfVo);
    }

    @ApiOperation(value = "资质新增")
    @PostMapping("/add")
    @RequiresPermissions("sys:quali:save")
    public RR<Void> add(@RequestBody QualiConfigConfigDto dto) {
        iQualiConfService.add(dto);
        return RR.success();
    }

    @ApiOperation(value = "证书条件批量删除")
    @PostMapping("/cert/delete/batch")
    @RequiresPermissions("sys:quali:update")
    public RR<Void> qualiConfigCertDeleteBatch(@RequestBody QualiCertDeleteDto dto) {
        iQualiCertCondService.qualiConfigCertDeleteBatch(dto);
        return RR.success();
    }

    @ApiOperation(value = "证书条件删除")
    @GetMapping("/cert/delete/{id}")
    @RequiresPermissions("sys:quali:update")
    public RR<Void> qualiConfigCertDelete(@PathVariable("id") Long id) {
        iQualiCertCondService.qualiConfigCertDelete(id);
        return RR.success();
    }

    @ApiOperation(value = "证书条件编辑")
    @PostMapping("/cert/edit")
    @RequiresPermissions("sys:quali:update")
    public RR<QualiConfigCertConfigVo> qualiConfigCertEdit(@RequestBody QualiConfigCertConfigDto dto) {
        QualiConfigCertConfigVo result = iQualiCertCondService.qualiConfigCertEdit(dto);
        return RR.successData(result);
    }

    @ApiOperation(value = "证书条件添加")
    @PostMapping("/cert/add")
    @RequiresPermissions("sys:quali:update")
    public RR<QualiConfigCertConfigVo> qualiConfigCertAdd(@RequestBody QualiConfigCertConfigDto dto) {
        QualiConfigCertConfigVo result = iQualiCertCondService.qualiConfigCertConfig(dto);
        return RR.successData(result);
    }
}
